package server

import (
	"onerk4/zg5yk/ordering/common/dao/mysql"
	"onerk4/zg5yk/ordering/common/dao/redis"
	"onerk4/zg5yk/ordering/common/global"
	"onerk4/zg5yk/ordering/common/model"
	"onerk4/zg5yk/ordering/common/proto/takeout"
)

func ShowTakeoutList(req *takeout.ShowTakeoutListRequest) (*takeout.ShowTakeoutListResponse, error) {
	query := global.DB.Model(&model.Takeout{})

	if req.IsPremium != 0 {
		query = query.Where("is_premium = ?", req.IsPremium)
	}

	if req.IsHot != 0 {
		query = query.Where("is_hot = ?", req.IsHot)
	}

	if req.IsSetMenu != 0 {
		query = query.Where("is_set_menu = ?", req.IsSetMenu)
	}

	list, err := redis.QueryShowTakeoutList(query)
	if err != nil {
		return nil, err
	}

	var items []*takeout.TakeoutItem

	for _, item := range list {
		items = append(items, &takeout.TakeoutItem{
			Id:                int64(item.Id),
			MealProductsName:  item.MealProductsName,
			MealProductsImage: item.MealProductsImage,
			MealProductsPrice: float32(item.MealProductsPrice),
			MealProductsStock: int64(item.MealProductsStock),
			IsHot:             int64(item.IsHot),
			IsPremium:         int64(item.IsPremium),
			IsSetMenu:         int64(item.IsSetMenu),
			Status:            int64(item.Status),
		})
	}

	return &takeout.ShowTakeoutListResponse{List: items}, nil
}

func UserRecommend(req *takeout.UserRecommendRequest) (*takeout.UserRecommendResponse, error) {
	recommend, err := mysql.UserRecommend(req.UserId)
	if err != nil {
		return nil, err
	}

	var items []*takeout.TakeoutItem

	for _, item := range recommend {
		items = append(items, &takeout.TakeoutItem{
			Id:                int64(item.Id),
			MealProductsName:  item.MealProductsName,
			MealProductsImage: item.MealProductsImage,
			MealProductsPrice: float32(item.MealProductsPrice),
			MealProductsStock: int64(item.MealProductsStock),
			IsHot:             int64(item.IsHot),
			IsPremium:         int64(item.IsPremium),
			IsSetMenu:         int64(item.IsSetMenu),
			Status:            int64(item.Status),
		})
	}

	return &takeout.UserRecommendResponse{List: items}, nil
}
